//
//  XSUserAccount.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/5.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

//返回的数据
//"access_token": "ACCESS_TOKEN",
//"expires_in": 1234,
//"remind_in":"798114",
//"uid":"12341234"
class XSUserAccount: NSObject ,NSCoding{

    // 用户的id
    var uid: String?
    // 生命周期(秒数) --> 从accessToken获取回来的那一刻开始计算, expires_in 这么多秒之后过期
    var expires_in: Double = 0 {
        didSet{
            // expiresDate = 当前时间 + expires_in(秒)
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    
    // 代表过期时间
    var expiresDate: NSDate?
    
    // 访问令牌
    var access_token: String?
    // 过期时间
    var remind_in: String?

    // 当前用户的昵称
    var screen_name: String?
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?

    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        //
    }
    
    override var description : String{
        
        let keys = ["access_token","expires_in","remind_in","uid"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder)  {
//        // 用户的id
//        var uid: String?
//        // 生命周期(秒数)
//        var expires_in: Int = 0
//        // 访问令牌
//        var access_token: String?
//        // 过期时间
//        var remind_in: String?
//        // 当前用户的昵称
//        var screen_name: String?
//        // 用户头像地址（大图），180×180像素
//        var avatar_large: String?
        
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(remind_in, forKey: "remind_in")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")

    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
    }
}
